package main

import "fmt"

func productExceptSelf(nums []int) []int {
	n := len(nums)
	numsProductLR := make([]int, n)
	numsProductRL := make([]int, n)
	numsProductRL[n-1] = 1
	for i := n - 2; i >= 0; i-- {
		numsProductRL[i] = numsProductRL[i+1] * nums[i+1]
	}
	productLast := 1
	for i := 0; i < n; i++ {
		if i == 0 {
			numsProductLR[0] = numsProductRL[0]
		} else {
			productLast *= nums[i-1]
			numsProductLR[i] = productLast * numsProductRL[i]
		}
	}

	return numsProductLR
}

func productExceptSelf2(nums []int) []int {
	n := len(nums)
	numsProduct := make([]int, n)
	numsProduct[n-1] = 1
	for i := n - 2; i >= 0; i-- {
		numsProduct[i] = numsProduct[i+1] * nums[i+1]
	}
	productLast := 1
	for i := 1; i < n; i++ {
		productLast *= nums[i-1]
		numsProduct[i] = productLast * numsProduct[i]
	}

	return numsProduct
}

func main() {
	fmt.Println([]int{1, 2, 3, 4})
	fmt.Println(productExceptSelf([]int{1, 2, 3, 4}))
	fmt.Println(productExceptSelf2([]int{1, 2, 3, 4}))
}
